package com.bocloud.cmp.dao.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.bocloud.cmp.dao.ApplyRecordDao;
import com.bocloud.cmp.entity.ApplyRecord;
import com.bocloud.common.model.Param;
import com.bocloud.common.utils.MapTools;
import com.bocloud.database.core.intf.impl.JdbcGenericDao;
import com.bocloud.database.utils.SQLHelper;

/**
 * 申请记录DAO接口实现
 * 
 * @author dongkai
 *
 */
@Repository("applyRecordDao")
public class ApplyRecordDaoImpl extends JdbcGenericDao<ApplyRecord, Long> implements ApplyRecordDao {

	@Override
	public List<ApplyRecord> list(int page, int rows, List<Param> params, Map<String, String> sorter) throws Exception {
		String sql = "select * from apply_record a where a.is_deleted = 0";
		sql = SQLHelper.buildRawSql(sql, page, rows, params, sorter, "a");
		return this.list(ApplyRecord.class, sql);
	}

	@Override
	public List<ApplyRecord> done(int page, int rows, List<Param> params, Map<String, String> sorter, Long userId)
			throws Exception {
		StringBuffer sqlBuffer = new StringBuffer("SELECT apply_record.* from apply_record");
		sqlBuffer.append(" left JOIN approve_node ON approve_node.apply_id = apply_record.id ")
				.append("where (approve_node.`status`='APPROVED' or approve_node.`status`='REFUSED')")
				.append("and approve_node.is_deleted=0 and apply_record.is_deleted=0 ")
				.append("and approve_node.approver = :approver ");
		String sql = SQLHelper.buildSql(sqlBuffer.toString(), page, rows, params, sorter, "apply_record");
		Map<String, Object> param = SQLHelper.getParam(params);
		param.put("approver", userId);
		return this.list(ApplyRecord.class, sql, param);
	}

	@Override
	public int countDone(List<Param> params, Long userId) throws Exception {
		StringBuffer sqlBuffer = new StringBuffer("SELECT count(*) from apply_record");
		sqlBuffer.append(" left JOIN approve_node ON approve_node.apply_id = apply_record.id ")
				.append("where (approve_node.`status`='APPROVED' or approve_node.`status`='REFUSED')")
				.append("and approve_node.is_deleted=0 and apply_record.is_deleted=0 ")
				.append("and approve_node.approver = :approver ");
		String sql = SQLHelper.buildRawSql(sqlBuffer.toString(), params, null, "apply_record");
		Map<String, Object> param = SQLHelper.getParam(params);
		param.put("approver", userId);
		return this.countQuery(sql, param).intValue();
	}

	@Override
	public int count(List<Param> params) throws Exception {
		String sql = "select count(*) from apply_record a where a.is_deleted = 0";
		sql = SQLHelper.buildRawSql(sql, params, null, "a");
		Map<String, Object> param = SQLHelper.getParam(params);
		return this.countQuery(sql, param).intValue();
	}

	@Override
	public boolean delete(Long id, Long userId) throws Exception {
		String sql = "update apply_record set is_deleted = 1, gmt_modify = :gmtModify, mender_id = :menderId where id = :id";
		Map<String, Object> params = MapTools.simpleMap("id", id);
		params.put("gmtModify", new Date());
		params.put("menderId", userId);
		return this.execute(sql, params) > 0;
	}

	@Override
	public ApplyRecord query(Long id) throws Exception {
		String sql = "select a.*, b.name applicantName from apply_record a left join user b on a.applicant = b.id where a.id = :id and a.is_deleted = 0";
		Map<String, Object> paramMap = MapTools.simpleMap("id", id);
		List<ApplyRecord> list = this.list(ApplyRecord.class, sql, paramMap);
		if (list.isEmpty()) {
			return null;
		}
		return list.get(0);
	}
}
